using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AppManagement.Repositories
{
	#region using
	using AppManagement.Models;
	using AppManagement.Helpers;
	#endregion

	public class GiangVienRepository
	{
		public static List<GiangVien> SelectAll()
		{
			try
			{
                var query = from item in DataContext.Instance.GiangVien
                            group item by item.MaKhoa into g
                            select g.OrderBy(n => n.HoTen);
                var lst = new List<GiangVien>();
                foreach (var groupGV in query)
                {
                    foreach (var gv in groupGV)
                    {
                        lst.Add(gv);
                    }
                }
                return lst;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<GiangVien>();
		}

		public static GiangVien SelectByID(int magv)
		{
			try
			{
				return DataContext.Instance.GiangVien.FirstOrDefault(item => item.MaGV == magv );
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return null;
		}

		public static bool Insert(GiangVien obj)
		{
			try
			{
				DataContext.Instance.GiangVien.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Delete(int magv)
		{
			try
			{
				var delitem = DataContext.Instance.GiangVien.FirstOrDefault(item => item.MaGV == magv );
				DataContext.Instance.GiangVien.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static List<GiangVien> RetrieveByID(int magv)
		{
			try
			{
				return (from item in DataContext.Instance.GiangVien where  item.MaGV == magv  select item).ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<GiangVien>();
		}

		public static List<GiangVien> SelectByMaTD(int matd)
		{
			try
			{
			var lstItem = (from item in DataContext.Instance.GiangVien where item.MaTD == matd select item).ToList();
			return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<GiangVien>();
		}

		public static List<GiangVien> SelectByMaKhoa(int makhoa)
		{
			try
			{
			var lstItem = (from item in DataContext.Instance.GiangVien where item.MaKhoa == makhoa select item).ToList();
			return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<GiangVien>();
		}

	}

}
